<template>
  <el-dialog :title="title" width="600px" append-to-body :visible.sync="dialogedit" @open="openDialog"
    :before-close="closeDialog" :close-on-click-modal="false" v-dialogDrag>
    <div class="form-box">
      <el-form ref="formdata" :model="ruleform" :rules="rules" label-suffix="：" :validate-on-rule-change="false"
        :show-message="false" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="参数名称" prop="config_name"><el-input
                v-model="ruleform.config_name"></el-input></el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参数键名" prop="config_key"><el-input
                v-model="ruleform.config_key"></el-input></el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参数键值" prop="config_value"><el-input
                v-model="ruleform.config_value"></el-input></el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" :rows="5" v-model="ruleform.remark"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="系统内置" prop="config_type">
              <el-radio-group v-model="ruleform.config_type">
                <el-radio v-for="dict in dict.type.sys_yes_no" :key="dict.value"
                  :label="dict.value">{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="save_validate()">保 存</el-button>
      <el-button @click="closeDialog">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import {
    saveConfig
  } from '@/api/system/config';
  export default {
    name: 'config_edit',
    dicts: ['sys_yes_no'],
    props: {
      dialogedit: {
        type: Boolean,
        default: false
      },
      ruleform: {
        type: Object,
        default: () => {
          return {
            config_id: 0,
            config_name: '',
            config_key: '',
            config_value: '',
            config_type: '',
            remark: ''
          };
        }
      }
    },
    data() {
      return {
        title: '修改参数',
        rules: {
          config_name: [{
            required: true,
            message: '参数名称不能为空'
          }],
          config_key: [{
            required: true,
            message: '参数键名不能为空'
          }],
          config_value: [{
            required: true,
            message: '参数键值不能为空'
          }],
          config_type: [{
            required: true,
            message: '系统内置不能为空'
          }]
        }
      };
    },
    created() {},
    methods: {
      closeDialog() {
        this.$nextTick(() => {
          this.$refs.formdata.resetFields();
          this.$emit('update:dialogedit', !this.dialogedit);
        });
      },
      save_validate() {
        this.$refs.formdata.validate((valid, valids) => {
          if (valid) {
            this.save_config();
          } else {
            var msg = '';
            for (var key in valids) {
              var item = valids[key];
              msg = item[0].message;
              break;
            }
            if (msg) this.$message.error(msg);

            return false;
          }
        });
      },

      async save_config() {
        
        const loading = this.loading('保存中...');
        saveConfig(this.ruleform).then(response => {
          this.$modal.msgSuccess("保存成功");
          loading.close();
          this.closeDialog();
          this.$emit('reload');
        }).catch(err => {
          loading.close();
        });
      },
      async openDialog() {
        if (this.ruleform.config_id == 0) {
          this.title = "修改参数";
        } else {
          this.title = "新增参数";
        }
      }
    }
  };
</script>

<style scoped>
  .form-box {
    min-height: 60px;
    padding-right: 10px;
    overflow: auto;
  }


</style>
